package com.softer.wxzj.common.flow.util;

import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.common.util.DateUtils;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.UUIDUtil;
import com.softer.wxzj.entity.FlowAuditdetail;
import com.softer.wxzj.entity.FlowConfig;
import com.softer.wxzj.entity.FlowInstance;
import com.softer.wxzj.mapper.FlowAuditdetailMapper;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import com.softer.wxzj.mapper.SysOperatorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
/**   
  *  
  * @ClassName: FlowAuditUtil   
  * @Description:  流程类型通用工具类  
  * @Author:    zcz  
  * @CreateDate:   [${date} ${time}]    
  */

public class FlowAuditUtil {

    @Autowired
    private FlowConfigMapper flowConfigMapper;
    @Autowired
    private FlowAuditdetailMapper flowAuditdetailMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private SysOperatorMapper sysOperatorMapper;
    /**
     * 初始化bean文件
     */
    public static FlowAuditUtil flowUtil;

    @PostConstruct
    public void init() {
        flowUtil = this;
        flowUtil.flowConfigMapper = this.flowConfigMapper;
        flowUtil.flowAuditdetailMapper = this.flowAuditdetailMapper;
        flowUtil.flowInstanceMapper = this.flowInstanceMapper;
        flowUtil.sysOperatorMapper = this.sysOperatorMapper;
    }

    /**
     * 保存实例表
     *
     * @param flowConfig
     * @param businessId
     * @return
     */
    int saveFlowInstance(String uuid, FlowConfig flowConfig, String businessId, String flowType, String modifyId, String operNo) {
        FlowInstance instance = new FlowInstance();
        instance.setId(uuid);
        instance.setCreater(operNo != null ? operNo : ShiroUserUtil.getUserName());
        instance.setConfigId(flowConfig.getId());
        instance.setBusinessId(businessId);
        instance.setFlowType(flowType);
        instance.setModifyId(modifyId);
        instance.setState(String.valueOf(NumberEnum.ZERO.getNum()));
        return flowInstanceMapper.insert(instance);
    }

    /**
     * 查询流程实例
     *
     * @param businessId
     * @return
     */
    FlowInstance getInstance(String businessId) {
        FlowInstance flowInstance = flowInstanceMapper.getInfoByFlowTypeAnBussId(businessId);
        return flowInstance;
    }

    /**
     * 保存记录表
     *
     * @param flowInstanceInfo
     * @param opinion
     */
    int saveFlowAuditdetail(FlowInstance flowInstanceInfo, String opinion, FlowConfig flowConfig, String flowState, String operNo) {
        FlowAuditdetail auditDetail = new FlowAuditdetail();
        auditDetail.setCreater(operNo != null ? operNo : ShiroUserUtil.getUserName());
        auditDetail.setBusinessId(flowInstanceInfo.getBusinessId());
        auditDetail.setFlowStep(flowConfig.getFlowStep());
        auditDetail.setRoleCode(flowConfig.getRoleCode());
        auditDetail.setOpinion(opinion);
        auditDetail.setFlowType(flowConfig.getFlowType());
        auditDetail.setInstanceId(flowInstanceInfo.getId());
        auditDetail.setFlowState(flowState);
        return flowAuditdetailMapper.insert(auditDetail);
    }

    /**
     * 更新审核实例为新节点
     *
     * @param flowInstanceInfo
     * @param flowConfig
     * @return
     */
    int updateFlowInstance(FlowInstance flowInstanceInfo, FlowConfig flowConfig) {
        FlowInstance instance = new FlowInstance();
        instance.setId(flowInstanceInfo.getId());
        instance.setConfigId(flowConfig.getId());
        return flowInstanceMapper.update(instance);
    }

    /**
     * 查询账号待审核的业务id和区域
     *
     * @param flowType
     * @return
     */
    public Map<String, String> getBussIdAndZoneCodeByOperNo(String flowType) {
        return flowInstanceMapper.getBussIdAndZoneCodeByOperNo(ShiroUserUtil.getUserName(), flowType);
    }

    /**
     * 查询外网账号待审核的业务id和区域
     * （业委会/居委会审核应急项目时）
     *
     * @param operNo   登录账号
     * @param flowType 业务类型
     * @return
     */
    public Map<String, String> getBussIdAndZoneCodeByOperNo(String operNo, String flowType) {
        return flowInstanceMapper.getBussIdAndZoneCodeByOperNo(operNo, flowType);
    }

    /**
     * Instance初始化通用方法
     *
     * @param businessId
     * @param flowType
     * @return
     */
    String initInstance(String businessId, String flowType, String modifyId, String operNo) {
        //查询当前登录人对应的角色是否在流程配置中
        int configCount = flowConfigMapper.getCountByOperNoAndFlowType(ShiroUserUtil.getUserName(), flowType);
        FlowConfig flowConfig;
        String flowState = FlowStateEnum.IN_AUDIT.getState();
        if (configCount > 0) {
            //根据账号和类型查询下一条配置
            flowConfig = flowConfigMapper.getFlowConfigByOperNoAndFlowType(ShiroUserUtil.getUserName(), flowType);
            //下一条配置不存在则赋状态为审批通过
            if (null == flowConfig) {
                flowState = FlowStateEnum.PASS.getState();
            }
        } else {
            //根据类型查询配置表第一条记录
            flowConfig = flowConfigMapper.getInfoByFlowType(flowType);
        }
        //保存实例表
        String uuid = UUIDUtil.getUUID();
        if (null != flowConfig) {
            saveFlowInstance(uuid, flowConfig, businessId, flowType, modifyId, operNo);
        } else {
            flowState = FlowStateEnum.PASS.getState();
            //默认流程表
            saveDefaultFlowInstance(uuid, businessId, flowType, modifyId, operNo);
            //默认意见表
            saveDefaultFlowAuditdetail(businessId, uuid, flowType, operNo);
        }
        return flowState;
    }

    /**
     * 默认审批意见
     *
     * @param businessId
     * @param instanceId
     * @param flowType
     * @return
     */
    int saveDefaultFlowAuditdetail(String businessId, String instanceId, String flowType, String operNo) {
        FlowAuditdetail auditdetail = new FlowAuditdetail();
        String operator = operNo != null ? operNo : ShiroUserUtil.getUserName();
        auditdetail.setCreater(operator);
        auditdetail.setBusinessId(businessId);
        auditdetail.setFlowStep(1);
        auditdetail.setRoleCode(sysOperatorMapper.getRoleCodeByOperNo(operator));
        auditdetail.setOpinion("审核通过");
        auditdetail.setFlowType(flowType);
        auditdetail.setInstanceId(instanceId);
        auditdetail.setFlowState(FlowStateEnum.PASS.getState());
        return flowAuditdetailMapper.insert(auditdetail);
    }

    public int saveDefaultFlowInstance(String uuid, String businessId, String flowType, String modifyId, String operNo) {
        FlowInstance instance = new FlowInstance();
        instance.setId(uuid);
        instance.setCreater(operNo != null ? operNo : ShiroUserUtil.getUserName());
        instance.setBusinessId(businessId);
        instance.setModifyId(modifyId);
        instance.setFlowType(flowType);
        instance.setState(NumStrEnum.ONE.getNum());
        instance.setEndTime(DateUtils.getNow());
        return flowInstanceMapper.insert(instance);
    }
}
